import os
import os.path
import glob

basedir = '/usr/local'

# C++ flags
debug = int(ARGUMENTS.get('debug', 0))
mm = ARGUMENTS.get('mm', 'no')
ccflags = "-Wall -pipe -fPIC "
suffix = "-mm"

if os.uname()[-1] == 'x86_64':
	arch = '-march=k8'
else:
	arch = '-march=k8 -m32 '

if mm != 'yes':
	ccflags += '-DNO_CMM '
	suffix = ""

if debug == 1:
	ccflags += '-g -fno-inline'
else:
	ccflags += '-Os %s' % arch
	if debug == 2:
		ccflags += ' -g -fno-inline'


# Base environment

env = Environment(CCFLAGS = ccflags, CPPPATH = ['include', './'])
objs = glob.glob('*.cpp')
includes = glob.glob('include/*.h')

# Compile the remaining sources and generate the program
libsimfw = env.SharedLibrary('libpbsfw%s' % suffix, source = objs)

# install
libdir = '%s/lib' % basedir
incdir = '%s/include/pbsfw' % basedir
env.Install(libdir, libsimfw)
env.Install(incdir, includes)
env.Alias('install', [incdir, libdir])

# Extra files to clean
base = os.path.abspath(os.path.curdir)
backups = glob.glob('%s/*~*' % base)
outfiles = glob.glob('%s/out*' % base)
massif = glob.glob('%s/massif*' % base)
callgrind = glob.glob('%s/callgrind*' % base)
cleanup = [backups, outfiles, massif, callgrind]

env.Clean(libsimfw, cleanup)
env.Clean('trash', cleanup)
